<template>

  <div class="login-container" >
    <br>
    <h2>用户登录</h2>
    <!-- 登录表单区域 -->
    <el-form :model="admin" :rules="userRules" label-width="0px" ref="userForm">
      <el-form-item  prop="userName">
      <el-input v-model="admin.userName" placeholder="输入您的用户名" prefix-icon="el-icon-user-solid"></el-input>
      </el-form-item>
      <el-form-item prop="userPass">
      <el-input placeholder="输入您的密码" prefix-icon="el-icon-lock" type="password" v-model="admin.userPass"></el-input>
      </el-form-item>
      <el-form-item class="btns">
      <el-button @click="login" type="primary">登录</el-button>
      <el-button @click="resetForm" type="info">重置</el-button>
      </el-form-item>
    </el-form>
    </div>
    </template>

    <script>
export default {
  name: "Login",
  data() {
    return {
      admin: {
        userName: '',
        userPass: ''
      },
      userRules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        userPass: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6,max:16,message: '请输入6位到16位字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    login:function(){
      this.$refs['userForm'].validate((valid) => {
        if (!valid) {
          return
        }else {
          this.$http.post('login/teacherLogin?userName='+this.admin.userName+'&userPass='+this.admin.userPass).then((res)=>{
            if (res.data.success){
              this.$message({
                message: res.data.message,
                type: 'success'
              });
              console.log(res)

              var id = res.data.result.tbTeacher.id;
              var token = res.data.result.token
              var name = res.data.result.tbTeacher.userName

              localStorage.setItem("token",token)
              localStorage.setItem("id",id)
              localStorage.setItem("name",name);

              this.$router.push({ name: 'Hello'})
            }else {
              this.$message({
                message: res.data.message,
                type: 'error'
              });
            }
          })
        }
      })
    },
    resetForm: function () {
      this.$refs['userForm'].resetFields();
    }
  }
}
</script>

<style>
.login-container{
  width: 450px;
  height: 314px;
  background-color: #fff;
  position: absolute;
  top: 25%;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 5px;
}

  el-input{
    width: 300px;
  }

</style>
